package com.firebugsoft.metadata.dubbo.impl.service;

import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.firebugsoft.metadata.dubbo.api.IRegionService;
import com.firebugsoft.metadata.dubbo.api.to.RegionTO;
import com.firebugsoft.metadata.dubbo.impl.dao.RegionDAO;
import com.firebugsoft.metadata.dubbo.impl.po.RegionPO;

@Service
public class RegionService implements IRegionService {
    @Resource
    private RegionDAO regionDao;

    @Override
    public RegionTO findByCode(String code) {
        RegionPO po = regionDao.findByCode(code);
        return po2to(po, new RegionTO());
    }

    @Override
    public List<RegionTO> findByPcode(String pcode) {
        List<RegionPO> pos = regionDao.findByPcode(pcode);
        List<RegionTO> tos = new ArrayList<>(pos.size());
        for (RegionPO po : pos) {
            tos.add(po2to(po, new RegionTO()));
        }
        return tos;
    }

    private RegionTO po2to(RegionPO po, RegionTO to) {
        to.setCode(po.getCode());
        to.setPcode(po.getPcode());
        to.setName(po.getName());
        to.setLevel(po.getLevel());
        to.setSimpleSpell(to.getSimpleSpell());
        to.setFirstSpell(to.getFirstSpell());
        to.setIntactSpell(to.getIntactSpell());
        return to;
    }
}
